﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Windows.Forms;

namespace Boggle {

    /// <summary>
    /// Window to load word and dice files. 
    /// Filenames are stored in BoggleWindow object.
    /// <BR>
    /// Author: David I. Schwartz (dis@mail.rit.edu)
    /// <BR>
    /// </summary>
    public partial class CustomGameWindow : Form {

        #region fields

        private BoggleWindow mainWindow; // link to BoggleWindow
        private string diceFileName;     // current dice filename
        private string wordFileName;     // current words filename

        /// <summary>
        /// Access new dictionary.
        /// </summary>
        public string WordFileName {
            set { wordFileName = value; }
            get { return wordFileName; }
        }

        /// <summary>
        /// Access mew dice file.
        /// </summary>
        public string DiceFileName {
            set { diceFileName = value; }
            get { return diceFileName; }
        }


        #endregion fields

        #region constructors

        /// <summary>
        /// Construct a FileLoadingWindow to access and store word/dice filenames.
        /// </summary>
        /// <param name="bw">Link to main window.</param>
        public CustomGameWindow(BoggleWindow bw) {

            // basic set up:
            InitializeComponent();
            mainWindow = bw;

            // set up this window to alert Boggle window:
            doneButton.DialogResult = DialogResult.OK;
            cancelButton.DialogResult = DialogResult.Cancel;

            // don't let the user finish until word/dice filenames chosen:
            doneButton.Enabled = false; 

        } // constructor FileLoadingWindow
        
        #endregion constructors

        #region button methods
        
        /// <summary>
        /// Access word list file and store filename in BoggleWindow object.
        /// </summary>
        /// <param name="sender">WordList button</param>
        /// <param name="e"></param>
        private void wordListButton_Click(object sender, EventArgs e) {
            wordFileName = updateFileName(wordListLabel);
            checkIfDone();
        } 

        /// <summary>
        /// Access dice list file and store filename in BoggleWindow object.
        /// </summary>
        /// <param name="sender">DiceList button</param>
        /// <param name="e"></param>
        private void diceListButton_Click(object sender, EventArgs e) {
            diceFileName = updateFileName(diceListLabel);
            checkIfDone();
        }

        /// <summary>
        /// Close this window, setup new Game, and return control to main window.
        /// </summary>
        /// <param name="sender">Done button</param>
        /// <param name="e"></param>
        private void doneButton_Click(object sender, EventArgs e) {
            Dispose();
        }

        /// <summary>
        /// Cancel file loading if user doesn't want to finish
        /// </summary>
        /// <param name="sender">Cancel button</param>
        /// <param name="e"></param>
        private void cancelButton_Click(object sender, EventArgs e) {
            Dispose();
        }

        #endregion button methods

        #region filenames
        
        /// <summary>
        /// Update the file loading window label and main window file name
        /// for supplied dice or word file.
        /// </summary>
        /// <param name="label">Word/Dice label in file loading window</param>
        /// <param name="filename">Word/Dice filename in main window</param>
        private string updateFileName(Label label) {
            
            // open file dialog window:
            string newName = getFileName();

            // if user cancels or something else fails, don't store a file name:
            if (newName == null)
                label.Text = "Still need a file.";

            // otherwise, store filename and check if ready to allow user to finish:
            else {
                label.Text = Path.GetFileName(newName);
            }
            return newName;

        } // method updateFileName

        /// <summary>
        /// Helper method to show file loading window.
        /// </summary>
        /// <returns>Name of file selected by user.</returns>
        private string getFileName() {
            
            OpenFileDialog dlg = new OpenFileDialog(); 
            dlg.Filter = "txt files (*.txt)|*.txt";    // just text
            dlg.FilterIndex = 1;                       // only one option           
            dlg.RestoreDirectory = false;              // let player keep opening same directory

            if (dlg.ShowDialog() == DialogResult.OK)
                return dlg.FileName;

            return null;

        } // method getFileName

        /// <summary>
        /// Allow user to finish (show Finished Button) if both words/dice filenames set:
        /// </summary>
        private void checkIfDone() {
            if (wordFileName != null && diceFileName != null)
                doneButton.Enabled = true;
        } 

        #endregion filenames

    } // class FileLoadingWindow

} // Boggle
